package servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.LinkedList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Account;
import model.Connect;
import model.SQLModel;
import model.Sales;
import model.Table;
import model.UserInfo;
import model.table.DMLTableInterface;
import model.table.ItemTable;
import model.table.SaleReportTable;



/**
 * Servlet implementation class ServletLogin
 */
public class SubmitUserAudit extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SubmitUserAudit() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		try{

			if (request.getMethod().equalsIgnoreCase("post")||request.getMethod().equalsIgnoreCase("get")) {
				try {
					Connect connect = new Connect();
					Account account=connect.checkSession(request, response);
					if(!"admin_role".equalsIgnoreCase(account.strRole)){
						//send error
					}	
					connect.setSID(connect.getdbSession(request));
					connect.setUser(account.strUserName);
					connect.setPassword(account.strPass);
					Connection conn = connect.simple_connect();
				
	
					SQLModel db = new SQLModel(conn);
					//selectsc
					//LinkedList<Sales> lst= db.select();
					
				   
				    String grantee=request.getParameter("grantee");
				   
				    boolean isAudited = db.isAuditWhole();
				    LinkedList<Table>lst= db.getListAuditTables("admin", grantee);
				    String check=request.getParameter("audit");
				   
				    boolean isaudit= db.isAuditStandard(grantee);
				    if("on".equals(check)&&!isaudit){
				    	db.auditStandard(grantee,"whenever successful");
				    	isaudit=true;
				    }
				    else if(isaudit && check==null){
				    	db.noAuditStandard(grantee);
				    	isaudit=false;
				    }
				    for(int i =0; i<lst.size();i++){
				    	Table t = lst.get(i);
				    	 
				    	 if("on".equals(request.getParameter("audited"+i))){
				    		 if(!t.getAudit()){
				    			 db.createFGAPolicy("ADMIN", grantee, t.ownerName, t.tableName, "INSERT,DELETE,UPDATE,SELECT");
				    			 t.setAudit(true);
				    			 System.out.println("false -> true");
				    		 }
				    	 }
				    	 else{
				    		 
			    			 if(t.getAudit()){
			    				 t.setAudit(false);
			    				// System.out.println("DROP"+grantee+t.ownerName+t.tableName);
			    				 db.dropFGAPolicy("ADMIN", grantee,t.ownerName, t.tableName);
			    				
			    			 }
				    	 }
				    	
				    }
				    LinkedList<String>lst1= db.getAllUsers();  
				    conn.close();
				    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/JSP/adminView/userAuditing.jsp");
					request.setAttribute("lstUser", lst1);
					request.setAttribute("lstTable", lst);
					request.setAttribute("grantee",grantee);
					request.setAttribute("isaudit",String.valueOf(isaudit));
					request.setAttribute("isAudited", isAudited);
					dispatcher.forward(request,response);
					
				} catch (Exception e) {
					System.out.print("Connection unsuccessfly "+ e.getMessage());
					//response.sendRedirect("ManageAccount/Login.jsp");
				}
			}

		}catch(Exception ex){
			System.out.println(ex.getMessage());
		}
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request,response);
	}

}
